package com.lite.api.controller.publics.commodities;


import com.lite.business.convert.commodities.CommoditiesConvert;
import com.lite.business.entity.commodities.Commodities;
import com.lite.business.entity.commodities.CommodityQueryWrapper;
import com.lite.business.exception.BusinessException;
import com.lite.business.service.commodities.CommoditiesService;
import com.lite.common.dto.ResultResponse;
import com.lite.common.utils.ResultResponseUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author stranger
 * @since 2022-10-24
 */
@RestController
@RequestMapping("/commodities")
public class CommoditiesController {
    @Autowired
    CommoditiesService commoditiesService;

    @Autowired
    CommoditiesConvert commoditiesConvert;

    /**
     * 读取一个商品的信息
     */
    @GetMapping("/getCommodityById")
    public ResultResponse<Commodities> getCommodityById(
            @RequestParam @NotNull Integer cmodId) throws BusinessException {
        return ResultResponseUtils.success(commoditiesService.getCommodityById(cmodId), "获取商品信息成功");
    }

    /**
     * 分页读取商品信息列表
     */
    @PostMapping("/listCommoditiesByPage")
    public ResultResponse<List<Commodities>> listCommoditiesByPage(
            @RequestParam Integer page, @RequestParam Integer size, @RequestParam CommodityQueryWrapper commodityQueryWrapper) {
        return ResultResponseUtils.success(commoditiesService.listCommoditiesByPage(page, size, commodityQueryWrapper), "获取商品信息列表成功");
    }

    /**
     * 修改一个商品的信息
     */
    @PostMapping("/updateCommoditiesById")
    public ResultResponse<Boolean> updateCommoditiesById(
            @RequestParam @NotNull Integer cmodId, @RequestParam @NotNull Commodities newCommodity) throws BusinessException {
        return ResultResponseUtils.success(commoditiesService.updateCommoditiesById(cmodId, newCommodity), "修改商品信息成功");
    }

    /**
     * 删除一个商品
     */
    @DeleteMapping("/removeCommoditiesById")
    public ResultResponse<Boolean> removeCommoditiesById(@RequestParam @NotNull Integer cmodId) throws BusinessException {
        return ResultResponseUtils.success(commoditiesService.removeCommoditiesById(cmodId), "删除商品成功");
    }

    /**
     * 新增一个商品
     */
    @PostMapping("/addCommodities")
    public ResultResponse<Boolean> addCommodities(@RequestBody @NotNull Commodities commodities) throws BusinessException {
        return ResultResponseUtils.success(commoditiesService.addCommodities(commodities), "新增商品成功");
    }

}
